/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.prashant.sqlduniya.dashboard;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.LifecycleManager;
import org.openide.modules.ModuleInstall;
import org.openide.util.HelpCtx;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.prashant.sqlduniya.dbexplorer.DatabaseExplorerTopComponent;
import org.prashant.sqlduniya.sqlmanager.DBConnectionInfo;
import org.prashant.sqlduniya.sqlmanager.DBConnectionManager;
import org.prashant.sqlduniya.sqlmanager.DBNode;

public class Installer extends ModuleInstall
        implements ActionListener {

    private DBConnectionPanel panel = new DBConnectionPanel();
    private DialogDescriptor d = null;
    final String[] options = new String[]{"Connect", "Test" ,"Cancel"};

    @Override
    public void restored() {

        d = new DialogDescriptor(panel, "Connection", true,
                options, 0, 0, HelpCtx.DEFAULT_HELP, this);
        d.setClosingOptions(new Object[]{});
        d.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent e) {
                if (e.getPropertyName().equals(DialogDescriptor.PROP_VALUE)
                        && e.getNewValue() == DialogDescriptor.CLOSED_OPTION) {
                    LifecycleManager.getDefault().exit();
                }
            }
        });
//        DialogDisplayer.getDefault().notifyLater(d);
        WindowManager.getDefault().invokeWhenUIReady(new Runnable() {

            public void run() {
                DialogDisplayer.getDefault().notify(d);
            }
        });

    }

    public void actionPerformed(ActionEvent event) {
        if ("CANCEL".equalsIgnoreCase(event.getSource().toString())) {
            LifecycleManager.getDefault().exit();
        } else  if ("CONNECT".equalsIgnoreCase(event.getSource().toString())) {
            DBConnectionManager conManager = Lookup.getDefault().lookup(DBConnectionManager.class);            
            DBNode node = conManager.loadDB(panel.getSelectedConnection());
            if (node != null) {
                TopComponent dbTree = WindowManager.getDefault().findTopComponent("DBTreeTopComponent");
                if (dbTree != null) {
                    ((DBTreeTopComponent) dbTree).setdBNode(node);
                    if (!dbTree.isOpened()) {
                        dbTree.open();
                    }
                    dbTree.requestActive();
                    
                    DatabaseExplorerTopComponent dbExp = (DatabaseExplorerTopComponent) WindowManager.getDefault().findTopComponent("DatabaseExplorerTopComponent");
                    dbExp.initTree(node);

                    TopComponent tc = WindowManager.getDefault().findTopComponent("QueryEditorTopComponent");
                    if (tc != null) {
                        tc.open();
                        tc.requestActive();
                    }

                    tc = WindowManager.getDefault().findTopComponent("SQLOutputViewTopComponent");
                    if (tc != null) {
                        tc.open();
                        tc.requestActive();
                    }
                    d.setClosingOptions(null);
//                    //Trigger lookup
//                    setSelectedConnection(panel.getSelectedConnection());
                }
            } else {
                d.setClosingOptions(null);
            }
        }else  if ("TEST".equalsIgnoreCase(event.getSource().toString())) {            
            String status = ping(panel.getSelectedConnection());
            panel.setStatus(status);
        }
    }
    
    private String ping(DBConnectionInfo selectedConnection) {
        DBConnectionManager conManager = Lookup.getDefault().lookup(DBConnectionManager.class);
        return selectedConnection != null ? conManager.ping(selectedConnection):
                "No connection selected";
    }
}
